import {
    Actor,
    HttpAgent,
    // IDL,
    ActorSubclass,
} from '@dfinity/agent';
// import { InterfaceFactory } from '@dfinity/candid/lib/cjs/idl';

// Canister ID
// const canisterId = 'mxzaz-hqaaa-aaaar-qaada-cai';
const canisterId = 'spdsf-5yaaa-aaaam-adcnq-cai';
import { idlFactory } from "./icrc1_ledger_canister_backend.ts"; // 假设您已生成并导入IDL
import { Principal } from '@dfinity/principal';


// Initialize an agent to interact with the canister
const agent = new HttpAgent({
    host: 'https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.icp0.io', // Ensure this matches the network you're interacting with
});

// Uncomment this line if you are using a local replica
agent.fetchRootKey();

// Create an actor with the specified canister ID and IDL
const canisterActor: ActorSubclass = Actor.createActor(idlFactory as any, {
    agent,
    canisterId,
});

// 定义查询余额的函数
async function getBalance(owner: string) {
    try {
      const principalOwner = Principal.fromText(owner);
      const balance = await canisterActor.icrc1_balance_of({
        owner: principalOwner,
        subaccount: []
      });
      console.log(`Balance: ${balance}`);
      return balance;
    } catch (error) {
      console.error("Error fetching balance:", error);
      throw error;
    }
  }

const ownerPrincipal = "2tvod-p3ywk-u576m-6ur5l-qryqm-pljuc-uk6ro-otl5f-ewtjh-sss2r-sae";
getBalance(ownerPrincipal);
